#!/usr/bin/env node

import * as gt from "getopt";
import { doTinyCompress } from "./tinypng";
import { KeeperService } from "./lib/Keeper";
import { readFileSync } from 'fs'
import { setConfig, getConfig } from "./lib/config";

let args = process.argv.splice(2, process.argv.length);

function helpFunc(opt: string) {
    let desc = '';
    switch (opt) {
        case 'M': desc = 'MongoDB uri 数据库地址  default 192.168.0.3'; break;
        case 'D': desc = 'MongoDB database 数据库名'; break;
        case 'P': desc = 'MongoDB port 数据库端口'; break;
        case 's': desc = 'pic src path 图片位置'; break;
        case 'd': desc = 'pic output path 图片导出位置'; break;
        case 'S': desc = 'sqlite 模式的数据库地址'; break;
        case 'r': desc = '重试次数'; break;
        case 't': desc = '每次请求压缩超时时间'; break;
        case 'K': desc = 'api keys tintpng的apikey 多的的时候用 ,分隔'; break;
        case 'C': desc = 'read config path'; break;
        case 'J': desc = 'open jpg 开启jpg图片压缩 默认只有png'
        case 'h': desc = 'help 帮助'; break;
    }

    return desc;
}

// if (args.length > 0) {
// let cfgInfo: any = {
//     mongoIp: '127.0.0.1',
//     mongoDb: 'tinypng',
//     mongoPort: '27017',
//     picSrc: '',
//     picDst: '',
//     sqlitePath: '',
//     ApiKeys: []
// }

gt.setopt("M:D:P:s:d:hK:C:S:r:t:J", args);
gt.getopt((opt: string, param: string) => {
    switch (opt) {
        case 'M': setConfig("mongoIp", param[0]); break;
        case 'D': setConfig("mongoDb", param[0]); break;
        case 'P': setConfig("mongoPort", param[0]); break;
        case 's': setConfig("picSrc", param[0]); break;
        case 'd': setConfig("picDst", param[0]); break;
        case 'S': setConfig("sqlitePath", param[0]); break;
        case 'K': setConfig("ApiKeys", (param[0] || "").split(',')); break;
        case 'r': setConfig("retry", parseInt(param[0] || "0")); break;
        case 'J': setConfig("jpgopen", true); break;
        case 't': setConfig("timeout", parseInt(param[0] || "0")); break;
        case 'C': {
            try {
                let cfg = JSON.parse(readFileSync(param[0]).toString());
                for (let key in cfg) {
                    setConfig(key, cfg[key]);
                }
            }
            catch (e) {

            }
            break;
        }
        case 'h': gt.showHelp('tinypng', helpFunc); break;
    }
});
async function start() {
    if (!getConfig("picDst") || !getConfig("picSrc")) {
        gt.showHelp('tinypng -s path -d path', helpFunc);
    }
    else {
        if (getConfig("sqlitePath")) {
            await KeeperService.init(getConfig("ApiKeys"), 'sqlite', getConfig("sqlitePath"))
        }
        else {
            await KeeperService.init(getConfig("ApiKeys"), 'mongo', getConfig("mongoIp"), getConfig("mongoPort"), getConfig("mongoDb"))
        }
        await doTinyCompress(getConfig("picSrc"), getConfig("picDst"))
        console.log('finish');
        process.exit(0);
    }
}

// if (process.argv.length == 2 && process.platform == 'win32') {
//     cfgInfo.picSrc = process.cwd();
//     cfgInfo.picDst = join(process.cwd(), '__tinypng__');
//     cfgInfo.sqlitePath = join(process.cwd(), '__tinypng__', 'image.db');
//     cfgInfo.ApiKeys = ['KD3QrDKGRbp0CdbcJcpSWSQCCSZVvl7Z'];
//     start();
// }
// else {
start();
// }
